<template>
  <div
    class="login-container flex-column-center rela"
    v-loading.fullscreen.lock="fullscreenLoading"
  >
    <div class="nav-container flex-row align-center abso" @click="handleNavIndex">
      <img class="logo" src="../index/img/5g-logo.png" alt="" />
      <div class="logo-text">省微波总站广电远程运维数字孪生平台</div>
    </div>
    <div class="login-title-container">
      <div class="title"></div>
      <div class="sub-title flex-row justify-between align-center">
        
      </div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form flex-column-center"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <div class="title">微波监测平台</div>
      </div>
      <el-form-item prop="account">
        <el-input
          v-model="loginForm.account"
          placeholder="用户账号"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        >
          <div slot="prefix" class="svg-container flex-column justify-center">
            <div class="flex-column-center">
              <svg-icon icon-class="user" />
            </div>
          </div>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
          @input="handleChangePassword"
        >
          <div slot="prefix" class="svg-container flex-column justify-center">
            <div class="flex-column-center">
              <svg-icon icon-class="password" />
            </div>
          </div>
          <div slot="suffix" class="show-pwd " @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </div>
        </el-input>
      </el-form-item>

      <el-checkbox-group
        v-model="businessPlatform"
        class="radio-group"
        @change="handleChangeBussinessPlatform"
      >
        <el-checkbox :label="BUSSINESS_PLATFORM.FRONT">前台</el-checkbox>
        <el-checkbox :label="BUSSINESS_PLATFORM.MANAGE">后台</el-checkbox>
      </el-checkbox-group>

      <el-button
        :loading="loading"
        type="primary"
        class="login-btn"
        @click.native.prevent="handleLogin"
      >
        登 录
      </el-button>
    </el-form>
  </div>
</template>

<script>
import md5 from 'js-md5'
// import { apiBindSceneId } from '@/api/dealRouter'
import { setLoginBussinessPlatform } from '@/utils/auth'
import { BUSSINESS_PLATFORM } from '@/consts'

export default {
  name: 'Login',
  filters: {
    passwordFormat(value) {
      return md5(value)
    },
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        account: 'admin_test',
        password: '1234.abcd',
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', message: '登录账号不能为空' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      fullscreenLoading: false,
      businessPlatform: [BUSSINESS_PLATFORM.FRONT],
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
    },
  },
  computed: {
    BUSSINESS_PLATFORM() {
      return BUSSINESS_PLATFORM
    },
  },

  mounted() {},
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (!this.businessPlatform.length) {
        this.$message.error('请先勾选要进入的系统')
        return
      }

      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', {
              account: this.loginForm.account,
              password: md5(this.loginForm.password),
              requestSourceEnum: this.businessPlatform[0],
            })
            .then(async () => {
              this.loading = false

              setLoginBussinessPlatform(this.businessPlatform[0])
              switch (this.businessPlatform[0]) {
                case BUSSINESS_PLATFORM.FRONT:
                  console.log(333)
                  window.location.replace(this.$consts.AccessUrl.frontUrl)
                  break
                case BUSSINESS_PLATFORM.MANAGE:
                  window.location.replace(this.$consts.AccessUrl.manageUrl)
                  break
              }

              // 首页系统
              // const sceneId = this.$route.query.sceneId
              // const isFront = this.$route.query.isFront
              // if (sceneId) {
              //   try {
              //     this.fullscreenLoading = true
              //     await apiBindSceneId(sceneId)
              //     // window.location.href = isFront
              //     //   ? this.$consts.AccessUrl.frontUrl
              //     //   : this.$consts.AccessUrl.manageUrl
              //     isFront
              //       ? window.location.replace(this.$consts.AccessUrl.frontUrl)
              //       : window.location.replace(this.$consts.AccessUrl.manageUrl)
              //   } catch (error) {
              //     console.log('handleNavFrontSystem', error)
              //   } finally {
              //     this.fullscreenLoading = false
              //   }
              // } else {
              //   this.$message.success('登陆成功！')
              //   this.$router.push('/')
              // }
            })
            .catch(() => {
              this.loading = false
              // this.$message.error(error)
            })
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      })
    },
    handleChangePassword(value) {
      this.loginForm.password = value
    },
    handleNavIndex() {
      this.$router.push('/')
    },
    handleChangeBussinessPlatform(value) {
      value.length && (this.businessPlatform = [value[value.length - 1]])
    },
  },
}
</script>

<style lang='scss'>
/* 修复input 背景不协调 和光标变色 */

/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #666;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
</style>

<style lang='scss' scoped>
$dark_gray: #889aa4;
$light_gray: #000;

.login-container {
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background: url('./img/login-bg.png') no-repeat;
  background-size: 100% 100%;

  .nav-container {
    top: 0;
    width: 100%;
    height: vh(80px);
    background: #fff;

    .logo {
      width: vw(48px);
      height: auto;
      margin-left: vw(50px);
    }

    .logo-text {
      margin-left: vw(8px);
      font-size: vw(23px);
      font-weight: 700;
      color: #333;
    }

    &:hover {
      cursor: pointer;
    }
  }

  .login-title-container {
    position: absolute;
    top: vh(278px);
    left: vw(90px);
    color: #fff;

    .title {
      font-size: vw(90px);
      font-style: italic;
      font-weight: 500;
      letter-spacing: vw(5px);
    }

    .sub-title {
      width: vw(792px);
      margin-top: vh(43px);
      font-size: vw(28px);
      color: #fff;
    }
  }

  .login-form {
    position: absolute;
    // width: vw(448px);
    // height: vw(382px);
    // padding: vw(45px) vw(54px);
    // margin: 0 auto;
    right: vw(194px);
    overflow: hidden;
    // background: white;
    // border-radius: 4px;
    // box-shadow: -2px -1px 8px 0 rgba(127, 127, 127, 0.91) inset;
    box-sizing: border-box;
    background: #fff;
    border-radius: 10px;
    padding: 40px;
  }

  .svg-container {
    // display: inline-block;
    width: vw(50px);
    height: 100%;
    // color: $dark_gray;
    // vertical-align: middle;

    .svg-icon {
      width: vw(24px);
      height: auto;
      // background: #fff;
      color: #4D6EE7;
    }
  }

  ::v-deep .el-input--prefix .el-input__inner {
    padding-left: vw(50px);
  }

  .title-container {
    position: relative;
    width: 100%;
    margin-bottom: vh(64px);

    .title {
      // margin: 0 auto 40px auto;
      font-size: vw(30px);
      font-weight: bold;
      color: #4D6EE7;
      text-align: center;
    }
  }

  .login-btn {
    width: vw(390px);
    height: vw(58px);
    margin-top: vh(40px);
    font-size: vw(20px);
    letter-spacing: vw(2px);
    background: #4D6EE7;
    border-radius: 10px;
  }

  .input {
    width: vw(390px);
    height: vw(58px);
    font-size: vw(20px);
  }

  .el-input {
    width: vw(390px);
  }

  ::v-deep .el-input__inner {
    height: vw(58px) !important;
    font-size: vw(27px);
    line-height: vw(58px);
    background: transparent;
    border: 1px solid #4D6EE7;
    border-radius: 10px;
  }

  ::v-deep .el-input__icon {
    height: auto;
    line-height: vw(58px);
  }

  ::v-deep .el-form-item__content {
    line-height: vw(58px);
  }

  ::v-deep .el-form-item {
    margin-bottom: vh(18px);
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.radio-group {
  width: vw(390px);
  // height: vw(68px);
  padding-left: vw(10px);
  text-align: left;
  box-sizing: border-box;
}

::v-deep {
  .show-pwd {
    position: absolute;
    right: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.radio-group {
  width: vw(390px);
  // height: vw(68px);
  padding-left: vw(10px);
  text-align: left;
  box-sizing: border-box;
}

::v-deep {
  .el-checkbox__input.is-checked + .el-checkbox__label {
    // font-size: vw(20px);
    color: #4D6EE7;
  }

  .el-checkbox__input.is-checked .el-checkbox__inner,
  .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background-color: #4D6EE7;
    border-color: #fff;
  }

  .el-checkbox {
    // font-size: vw(20px);
    color: #4D6EE7;
  }

  .el-checkbox__inner {
    // width: vw(20px);
    // height: vw(20px);
    background-color: transparent;

    &:hover {
      border: 1px solid #dcdfe6;
    }
  }

  .el-checkbox__input.is-focus .el-checkbox__inner {
    border-color: #dcdfe6;
  }
}
</style>
